﻿/*!
 *  Copyright (c), 上海沃恩信息科技有限公司.
 *  All rights reserved.
 *  Licensed under BSD (https://www.pittypat.work/bsd.html).
 */

using System.Threading.Tasks;
using Pittypat;

namespace Waln.Notifying.Unread
{
    /// <summary>
    /// 向客户端提供通知中心相关的功能。
    /// </summary>
    public sealed class NotificationService : INotificationService
    {
        private static readonly log4net.ILog Log = log4net.LogManager.GetLogger(typeof(NotificationService));

        private Api api;

        /// <summary>
        /// 初始化 <see cref="NotificationService"/> 类的新实例。
        /// </summary>
        public NotificationService()
        {
            this.api = new Api();
        }

        async Task<int64> INotificationService.CountAsync(uuid userId, IAsyncProgress progress)
        {
            var model = new FunctionModel<Args.Count>();
            model.Args.ReceiverId = userId;

            await this.api.CountAsync(model, progress);

            if (!model.Success)
            {
                Log.Error(model.Message);
            }

            return model.Args.Total ?? 0;
        }

        async Task<NotificationList> INotificationService.ListAsync(uuid userId, IAsyncProgress progress)
        {
            var model = new FunctionModel<Args.List>();
            model.Args.UserId = userId;

            await this.api.ListAsync(model, progress);

            if (!model.Success)
            {
                Log.Error(model.Message);
            }

            return new NotificationList(model.Args.Total ?? 0, Notification.From(model.Args.Notifications));
        }

        async Task<datetime?> INotificationService.ReadAsync(uuid userId, uuid notificationId, IAsyncProgress progress)
        {
            var model = new FunctionModel<Args.Read>();
            model.Args.UserId = userId;
            model.Args.Id = notificationId;

            await this.api.ReadAsync(model, progress);

            if (!model.Success)
            {
                Log.Error(model.Message);
            }

            return model.Args.ReadTime;
        }
    }
}
